package com.zuoshenge.main.controller;

import com.zuoshenge.main.dto.ImgUploadRequestDTO;
import com.zuoshenge.tool.Base64Utils;
import com.zuoshenge.util.UploadFileToRemoteUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("file")
public class FileController {

    @Value("${server-file.file-url}")
    private String fileUrl;

    @PostMapping("upload-image-base64")
    public ResponseEntity<Object> uploadImageBase64(@RequestBody ImgUploadRequestDTO imgUploadRequestDTO) throws IOException {
        MultipartFile multipartFile = Base64Utils.convertBase64ToMultipart(imgUploadRequestDTO.getImgBase64Code(), imgUploadRequestDTO.getImgName());
        UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile);
        Map<String, Object> res = new HashMap<>();
        res.put("status", "1");
        res.put("msg", "图片上传成功");
        res.put("imgUrl", fileUrl + imgUploadRequestDTO.getImgName());
        return ResponseEntity.ok(res);
    }

    @RequestMapping("/insertOneImageFile")
    public ResponseEntity<Object> uploadImageFile(@RequestBody ImgUploadRequestDTO imgUploadRequestDTO) throws IOException {
        MultipartFile multipartFile = imgUploadRequestDTO.getImgFile();
        UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile);
        Map<String, Object> res = new HashMap<>();
        res.put("status", "1");
        res.put("msg", "图片上传成功");
        res.put("imgUrl", fileUrl + imgUploadRequestDTO.getImgName());
        return ResponseEntity.ok(res);
    }
}
